package com.caomeisan.muzi.kcface;

/**
 * @ClassName ByteUtils
 * @Description TODO
 * @Date 2023/11/2714:37
 * @Version 1.0
 */
public class ByteUtils {
    public static byte[] float2Byte(float[] data) {
        byte[] r = new byte[data.length * 4];

        for (int i = 0; i < 256; ++i) {
            int v = Float.floatToIntBits(data[i]);
            r[i * 4] = (byte) (v & 255);
            r[i * 4 + 1] = (byte) (v >> 8 & 255);
            r[i * 4 + 2] = (byte) (v >> 16 & 255);
            r[i * 4 + 3] = (byte) (v >> 24 & 255);
        }

        return r;
    }

    public static float[] byte2Float(byte[] data) {
        float[] r = new float[data.length / 4];

        for (int i = 0; i < 256; ++i) {
            int v = data[i * 4] & 255 | (data[i * 4 + 1] & 255) << 8 | (data[i * 4 + 2] & 255) << 16 | (data[i * 4 + 3] & 255) << 24;
            r[i] = Float.intBitsToFloat(v);
        }
        return r;
    }


    public static double similarity(double cosm) {
        float[] var10000 = new float[]{0.0F, 0.713775F, 0.724375F, 0.734775F, 0.7399F, 0.756951F, 0.7696F, 1.0F};
        float[] srcPoint98y2 = new float[]{0.0F, 0.71537775F, 0.72699374F, 0.73219377F, 0.7373437F, 0.750999F, 0.763804F, 1.0F};
        var10000 = new float[]{0.0F, 0.7191F, 0.724375F, 0.734775F, 0.7399F, 0.753984F, 0.762831F, 1.0F};
        var10000 = new float[]{0.0F, 0.72594774F, 0.7311578F, 0.7399F, 0.743964F, 0.758919F, 0.77199376F, 1.0F};
        float[] srcPoint = srcPoint98y2;
        float[] dstPoint = new float[]{0.0F, 0.4F, 0.5F, 0.6F, 0.7F, 0.85F, 0.95F, 1.0F};
        double r = 0.0D;
        if (cosm >= 1.0D) {
            return 1.0D;
        } else if (cosm < 0.0D) {
            return 0.0D;
        } else {
            for (int i = 1; i < 8; ++i) {
                if (cosm < (double) srcPoint[i]) {
                    r = (double) dstPoint[i - 1] + (cosm - (double) srcPoint[i - 1]) * (double) (dstPoint[i] - dstPoint[i - 1]) / (double) (srcPoint[i] - srcPoint[i - 1]);
                    break;
                }
            }

            return r;
        }
    }




    public static float[] normalizeNew01(float[] vector) {
        double norm = 0.0;
        for (int i = 0; i < (vector.length - 1); i++) {
            norm += vector[i] * vector[i];
        }
        norm = Math.sqrt(norm);

        // Normalize the vector
        for (int i = 0; i < (vector.length - 1); i++) {
            vector[i] /= norm;
        }
        return vector;
    }
}
